package ui.custom.panel;

import java.awt.Color;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import java.io.IOException;
import java.text.ParseException;

import javax.swing.JTabbedPane;

import domain.MyTools;
import domain.valueObejcts.Project;

public class MonthTabs extends JTabbedPane {

	private static final long serialVersionUID = 1L;
	
	private String month;

	private String year;

	private Project project;
	
	public MonthTabs(String month, String year) {
		this.month = month;
		this.year = year;
		setForeground(new Color(0, 0, 0));
		init();
	}

	public MonthTabs(Project project) throws IOException, ParseException {
		this.project = project;
		this.month = project.getMonth();
		this.year = project.getYear();
		init();
		if(!project.getPositions().isEmpty()) update();
	}
	
	private void init() {
		setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
		
		addMouseWheelListener(new MouseWheelListener(){
			@Override
			public void mouseWheelMoved(MouseWheelEvent e) {
				int index = getSelectedIndex();
				if(e.getWheelRotation() < 0){
					if(index <= 0){
						index = 1;
					}
					setSelectedIndex(index - 1);
				} else {
					if(index >= getTabCount()-1){
						index = getTabCount()-2;
					}
					setSelectedIndex(index + 1);
				}
			}
		});
	}
	
	public String getMonth(){
		return this.month;
	}
	
	public String getYear(){
		return this.year;
	}
	
	private void update() throws IOException, ParseException {
		for (String date : MyTools.getMonthDays(MyTools.getMonthNumber(month), Integer.parseInt(year))) {
			NavigateFloors nav = new NavigateFloors(date, project.getPersonalPanel(), project);
			add(date, nav);
		}
	}

	@Override
	public String toString() {
		return this.year + "_" + this.month;
	}
	
}
